import Foundation

public protocol NetworkResultDelegate {
    associatedtype TypeData;

    func setData(_ data: TypeData?);

    func getData() -> TypeData?;

    func setCode(_ code: Int);

    func getCode() -> Int;

    func isSuccess() -> Bool;

    func getCodeOk() -> Int;
}

public protocol NetworkListResultDelegate: NetworkResultDelegate {
    associatedtype TypeListData;

    func add(item: TypeListData);
}

open class NetworkListResultEx<T>: NetworkListResultDelegate {
    public typealias TypeListData = T;
    public typealias TypeData = Array<TypeListData>;

    var data: Array<T>?;
    var code: Int = 0;
    var error: String?;

    public init() {
    }

    public init(_ code: Int) {
        self.code = code;
    }

    public func setData(_ data: Array<T>?) {
        self.data = data;
    }

    public func getData() -> Array<T>? {
        return data;
    }

    public func setCode(_ code: Int) {
        self.code = code;
    }

    public func getCode() -> Int {
        return code;
    }

    public func add(item: T) {
        if data == nil {
            data = Array();
        }
        data!.append(item);
    }

    open func getCodeOk() -> Int {
        fatalError(ConstantsEx.kNoImpl);
    }

    public func isSuccess() -> Bool {
        return code == getCodeOk();
    }
}

open class NetworkResultEx<T>: NetworkResultDelegate {

    public typealias TypeData = T;

    var code: Int = 0;
    var error: String = String();
    var data: T?;

    public init() {
    }

    public init(_ code: Int) {
        self.code = code;
    }

    public init(_ code: Int, err: String) {
        self.code = code;
        self.error = err;
    }

    public func setData(_ data: T?) {
        self.data = data;
    }

    public func getData() -> T? {
        return data;
    }

    public func setCode(_ code: Int) {
        self.code = code;
    }

    public func getCode() -> Int {
        return code;
    }

    public func isSuccess() -> Bool {
        return code == getCodeOk();
    }

    open func getCodeOk() -> Int {
        fatalError(ConstantsEx.kNoImpl);
    }
}

open class NetworkResp {
    var text: String?
    var stream: Stream?;
    var data: Data?;

    public func setText(_ t: String?) {
        text = t;
    }
    
    public func setData(_ d: Data?) {
        data = d;
    }

    public func getText() -> String {
        guard text != nil else {
            return String();
        }
        return text!;
    }
    
    public func getData() -> Data {
        guard data != nil else {
            return Data();
        }
        return data!;
    }
    
}
